﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class TimKiemTinTuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            if (!IsPostBack)
            {
                getData();
            }
            else
            {
                if (Request.Params["btn_Tim"] != null)
                {
                    getData();
                }
            }

            this.LoadComplete += new EventHandler(TimKiemTinTuc_LoadComplete);
        }

        void TimKiemTinTuc_LoadComplete(object sender, EventArgs e)
        {
            IServerWeb sw = new ServerWeb();
            DanhMucTinTuc_DAO dao = new DanhMucTinTuc_DAO(sw.Connection);
            List<DanhMucTinTuc> data = dao.getListByKey();
            data.Insert(0,
                new DanhMucTinTuc()
                {
                    MaDanhMuc = -1,
                    TenDanhMuc = "--- Tất cả ---"
                });

            Session.Add("page_TKTinTuc_dataListDM", data);
            sw.closeConnection();

        }
        private void Bind_Data()
        {
            data_GridView.DataSource = Session["page_TimKiemTinTuc_dataBD"];
            data_GridView.DataBind();
        }

        private void getData()
        {

            IServerWeb sw = new ServerWeb();
            TinTuc_DAO dao = new TinTuc_DAO(sw.Connection);
            string TenTinTuc = Request.Params["TenTinTuc"] != null ? Request.Params["TenTinTuc"] : "";
            int MaDanhMuc = int.Parse(Func.beginConvertDataType(Request.Params["MaDanhMuc"] != null ? Request.Params["MaDanhMuc"] : "-1", pDataType.Integer));

            List<TinTuc> listData = dao.getListByKey(TenTinTuc, MaDanhMuc);

            //for (int i = 0; i < listData.Count; i++)
            //{
            //    int len = listData[i].NoiDung.Length >= 200 ? 200 : listData[i].NoiDung.Length;
            //    listData[i].NoiDung = listData[i].NoiDung.Substring(0, len);
            //    //listData[i].MaTinhTrang = "(" + listData[i].MaTinhTrang + ") " + daoTT.getByKey(listData[i].MaTinhTrang).TenTinhTrang;
            //    //listData[i].TenNguoiNhan = daoKH.getByKey(listData[i].IDKhachHang).HoTen;
            //}

            Session.Add("page_TimKiemTinTuc_dataBD", listData);
            data_GridView.EditIndex = -1;
            Bind_Data();


            sw.closeConnection();

        }

        protected void data_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            data_GridView.PageIndex = e.NewPageIndex;
            Bind_Data();
        }

    }
}